<?php

namespace app\shop\controller;

use think\Controller;

class Business extends Controller
{
    // 控制器初始化
    public function _initialize()
    {
        parent::_initialize();
        // 实例化模型   
        $this->BusinessModel = model('Business.Business');
    }

    // 获取用户头像
    public function avatar()
    {
        $id = $this->request->param('id', 0, 'trim');

        $info = $this->BusinessModel->find($id);

        if (!$info) {
            return json(['code' => 0, 'msg' => '用户不存在']);
        }

        return json(['code' => 1, 'msg' => '', 'data' => $info['avatar_text']]);
    }

    // 图片上传
    public function uploads()
    {
        $id = $this->request->param('id', 0, 'trim');
        $info = $this->BusinessModel->find($id);

        if (!$info) {
            return json(['code' => 0, 'msg' => '用户不存在']);
        }

        $res = upload('avatar');

        if ($res['code'] == 0) {
            return json(['code' => 0, 'msg' => $res['msg']]);
        }

        return json(['code' => 1, 'msg' => '上传成功', 'data' => $res['data']]);
    }

    // 删除图片
    public function del()
    {
        $id = $this->request->param('id', 0, 'trim');
        $avatar = $this->request->param('avatar', '', 'trim');

        $info = $this->BusinessModel->find($id);

        if (!$info) {
            return json(['code' => 0, 'msg' => '用户不存在']);
        }

        is_file(trim($avatar, '/')) && unlink(trim($avatar, '/'));
    }
}
